﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LMIS_Application.books
{
    public partial class BookCheckout : System.Web.UI.Page
    {
        private List<Member> allMembers;
        private List<Book> allBooks;
        private int mSelection;
        private int bSelection;
        private DateTime dueDate;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetMemberList();
                SetBookList();
            }
        }

        protected void ddl_CO_Member_SelectedIndexChanged(object sender, EventArgs e)
        {
            mSelection = Convert.ToInt32(ddl_CO_Member.SelectedValue);
        }

        protected void ddl_CO_Book_SelectedIndexChanged(object sender, EventArgs e)
        {
            bSelection = Convert.ToInt32(ddl_CO_Book.SelectedValue);
        }

        protected void SetMemberList()
        {
            allMembers = Member.GetMemberList();
            ddl_CO_Member.DataSource = allMembers;
            ddl_CO_Member.DataTextField = "Name";
            ddl_CO_Member.DataValueField = "MemberId";
            ddl_CO_Member.DataBind();
        }

        protected void SetBookList()
        {
            allBooks = Book.GetBookList();
            ddl_CO_Book.DataSource = allBooks;
            ddl_CO_Book.DataTextField = "Title";
            ddl_CO_Book.DataValueField = "BookId";
            ddl_CO_Book.DataBind();
        }

        protected void cal_CO_DueDate_SelectionChanged(object sender, EventArgs e)
        {
            dueDate = cal_CO_DueDate.SelectedDate;
        }

        protected void btnBookCheckout_Click(object sender, EventArgs e)
        {
            string result = null;
            bool success = false;

            Member m = new Member();
            m.LoadMember(int.Parse(ddl_CO_Member.SelectedValue));

            Inv bkCopy = Inv.GetFirstAvailableCopy(int.Parse(ddl_CO_Book.SelectedValue));

            if (bkCopy != null)
            {
                success = bkCopy.Checkout(m, cal_CO_DueDate.SelectedDate);
                if (success)
                {
                    result = "The selected book was successfully checked out to " + m.Name + ", and is due back on " + cal_CO_DueDate.SelectedDate.ToShortDateString() + ".<br/>";
                    result += "The Inventory ID associated with this transaction is " + bkCopy.InvID.ToString();
                }
                else
                {
                    result = "The inventory transaction could not be completed at this time. Please try again at another time.";
                }
            }
            else
            {
                result = "There are no copies of this book currently in the inventory.";
            }


            msgSuccess.InnerHtml = result;
        }
    }
}
